<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .table {
            width: 400px;
            overflow: hidden;
            margin: 50px auto;
        }

        .table .item {
            float: left;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: blanchedalmond;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>
                用户名 <input type="text" v-model="username" />
            </div>
            <div>
                密码 <input type="text" v-model="userpwd" />
            </div>
            <button @click="add">新增</button>
        </div>
        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item  in   userList">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userpwd}}</div>
                <div class="item" @click="delById(item.id)">删除</div>
            </div>
        </div>

    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            userpwd: "",
            userList: [],
        },
        methods: {
            delById(id) {
                axios({
                    method: "delete",
                    url: "http://localhost:2345/userinfo/" + id,
                });//删除数据库中的数据
                this.userList = this.userList.filter(item => item.id != id);
                //同步页面数据
            },
            async add() {
                //判断用户名是否存在
                let   {data:list} = await  axios({
                    method:"get",
                    url:"http://localhost:2345/userinfo",
                    params:{
                        username:this.username
                    }
                })
                if(list.length){
                    alert("该用户名已经存在");
                    return false;
                }

                let {data} = await axios({//直接新增
                    method: "post",
                    url: "http://localhost:2345/userinfo",
                    data: {
                        username: this.username,
                        userpwd: this.userpwd
                    }
                });//新增到数据库
                this.userList.push(data);//新增到页面

            }
        },
        created() {
            axios({
                method: "get",
                url: "http://localhost:2345/userinfo"
            }).then(res => {
                this.userList = res.data;
            })
            //第一板 看network中xhr  里面的请求是否绿了  如果是pendding状态  就是json-server卡了 需要到json-server的位置 ctrl+c
            //第二板  看输出  看结果对不对
            //第三板   赋值给页面
        }
    })

</script>

</html>